package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname MyMQConfig
 * @Description TODO
 * @Date 2021/10/19 14:34
 * @Author Anonymous
 */
@SuppressWarnings("all")
@Configuration
public class MyMQConfig {

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
//        //手动确认，告诉已经成功
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        System.out.println("收到过期的订单，准备关闭订单...订单号是：" + entity.getOrderSn());
//    }

    /**
     * @Bean的作用： 容器中的Binding，Queue，Exchange都会自动创建（RabbitMQ没有的情况下）
     * RabbitMQ 只要有队列或者交换机，即使@Bean属性发生变化重启服务也不会覆盖，唯一的解决方案就是把队列或者交换机删除，重启服务
     */

    /**
     * Construct a new queue, given a name, durability flag, and auto-delete flag, and arguments.
     *
     * @param name       the name of the queue - must not be null; set to "" to have the broker generate the name.
     * @param durable    true if we are declaring a durable queue (the queue will survive a server restart)
     * @param exclusive  true if we are declaring an exclusive queue (the queue will only be used by the declarer's
     *                   connection)
     * @param autoDelete true if the server should delete the queue when it is no longer in use
     * @param arguments  the arguments used to declare the queue
     */

    //public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete,
    //        @Nullable Map<String, Object> arguments)
    @Bean
    public Queue orderDelayQueue() {
        /**
         *特殊属性：
         *  x-dead-letter-exchange: order-event-exchange
         *  x-dead-letter-routing-key: order.release.order
         *  x-message-ttl: 60000
         * */
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);//以毫秒为单位
        return new Queue("order.delay.queue", true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue", true, false, false, null);
    }

    @Bean
    public Exchange orderEventExchange() {
        //public TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
        return new TopicExchange("order-event-exchange", true, false, null);
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        //public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,
        //			@Nullable Map<String, Object> arguments)
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
    }

    /**
     * 订单释放和库存释放绑定
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.other", null);
    }


    /**
     * 商品秒杀队列
     */
    @Bean
    public Queue orderSecKillOrrderQueue() {
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }

    @Bean
    public Binding orderSecKillOrrderQueueBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			Map<String, Object> arguments
        Binding binding = new Binding(
                "order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
        return binding;
    }
}
